﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace AutumnEngine.Modules
{
    /// <summary>
    /// Manages mouse input.
    /// </summary>
    public class MouseInputModule : Module
    {        
        MouseState current, last;
        public override void Create()
        {
            current = Mouse.GetState();
            last = current;
            base.Create();
        }

        public override void Update()
        {
            last = current;
            current = Mouse.GetState();
            base.Update();
        }

        public Vector2 Position
        {
            get { return NoneTransformedPosition; }
        }

        /// <summary>
        /// Gets the position of the mouse.
        /// </summary>
        /// <value>The position.</value>
        public Vector2 NoneTransformedPosition
        {
            get
            {
                Vector2 ret;
                ret = new Vector2((float)current.X, (float)current.Y);
                return ret;
            }
            //Todo Set
        }

        public Vector2 TransformedPosition
        {
            get
            {
                return Vector2.Transform(NoneTransformedPosition, Matrix.Invert(Engine.Modules.SceneManager.ActiveScene.Camera.CameraMatrix));
            }
        }

        /// <summary>
        /// Gets a value indicating whether the left button is down.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is left button down; otherwise, <c>false</c>.
        /// </value>
        public bool IsLeftButtonDown
        {
            get { return current.LeftButton == ButtonState.Pressed; }
        }

        /// <summary>
        /// Gets a value indicating whether the left button is triggered.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is left button triggered; otherwise, <c>false</c>.
        /// </value>
        public bool IsLeftButtonTriggered
        {
            get { return current.LeftButton == ButtonState.Pressed && last.LeftButton == ButtonState.Released; }
        }

        /// <summary>
        /// Gets a value indicating whether the right button is down.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is right button down; otherwise, <c>false</c>.
        /// </value>
        public bool IsRightButtonDown
        {
            get { return current.RightButton == ButtonState.Pressed; }
        }

        /// <summary>
        /// Gets a value indicating whether the right button is triggered.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is right button triggered; otherwise, <c>false</c>.
        /// </value>
        public bool IsRightButtonTriggered
        {
            get { return current.RightButton == ButtonState.Pressed && last.RightButton == ButtonState.Released; }
        }

        public float CurrentMouseWheel
        {
            get { return current.ScrollWheelValue; }
        }

        public float LastMouseWheel
        {
            get { return last.ScrollWheelValue; }
        }

        public float MouseWheelVelocity
        {
            get { return CurrentMouseWheel - LastMouseWheel; }
        }
    }
}
